package com.zjsy.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zjsy.page.PageBean;
import com.zjsy.po.MainNewsList;
import com.zjsy.po.MainNewsModule;
import com.zjsy.po.Message;
import com.zjsy.po.WeChatNews;
import com.zjsy.po.WeChatNewsList;
import com.zjsy.service.MagazineService;
import com.zjsy.service.MainNewsService;
import com.zjsy.service.WeChatNewService;

@Controller
public class WeChatNewsController {
	
	@Autowired
	private WeChatNewService wechatNewService;
	@Autowired
	private MainNewsService mainNewsService;
	
	@RequestMapping("/wechat-list")
	public String weChatlList(HttpServletRequest req,HttpServletResponse resp) {
		

		int pc =getPc(req);
		String url=getUrl(req);
		PageBean<WeChatNewsList> List =wechatNewService.findWeChatNewsByPage(pc);
		List.setUrl(url);
		req.setAttribute("List", List);
		List<MainNewsModule> ClassifyList=mainNewsService.findNewsModule("3");
		req.setAttribute("ClassifyList", ClassifyList);
		return "wechat-list";
	}
	
	@RequestMapping("/addWechatNews")
	@ResponseBody
	public Message addWechatNew(HttpServletRequest req,HttpServletResponse resp, @RequestBody WeChatNews wechatNews) {
		wechatNewService.addWeChatNews(wechatNews);
		return new Message("添加微信新闻成功");
	}
	
	
	@RequestMapping("/updateWechatNews")
	@ResponseBody
	public Message updateWechatNew(HttpServletRequest req,HttpServletResponse resp, @RequestBody WeChatNews wechatNews) {
		wechatNewService.updateWeChatNewsById(wechatNews);
		return new Message("更新微信新闻成功");
	}
	
	@RequestMapping("/wechat-add")
	public String wechatadd(HttpServletRequest req,HttpServletResponse resp){
		
		List<MainNewsModule> ClassifyList=mainNewsService.findNewsModule("3");
		req.setAttribute("ClassifyList", ClassifyList);
		
		return "wechat-add";
		
	}
	
	@RequestMapping("/findWeChatNewsById")
	public String findWeChatNewsById(HttpServletRequest req,HttpServletResponse resp){
		
		String id= req.getParameter("id");
		WeChatNews wechanews =wechatNewService.findWechatNewsById(id);
		req.setAttribute("wechanews", wechanews);
		List<MainNewsModule> ClassifyList=mainNewsService.findNewsModule("3");
		req.setAttribute("ClassifyList", ClassifyList);
		
		return "wechat-edit";
		
	}
	
	@RequestMapping("/deleteWeChatNewsById")
	@ResponseBody
	public Message deleteWeChatNewsById(HttpServletRequest req,HttpServletResponse resp){
		
		String id= req.getParameter("id");
		
		wechatNewService.deleteWeChatNewsById(id);
		
		return new Message("删除成功");
		
	}
	
	
	
	
	
	

	
	private int getPc(HttpServletRequest req){
		int pc =1 ;
		String param = req.getParameter("pc");
		if(param!=null&&!param.trim().isEmpty()){
			try {
				pc = Integer.parseInt(param);
			} catch (RuntimeException e) {
				
			}
		}
		return pc;
	}
	//截取url，页面中的分页导航中要使用它作为超链接的目标
	private String getUrl(HttpServletRequest req){
		String url = req.getRequestURL()+"?"+req.getQueryString();
		//如果url中存在pc参数
		int index = url.lastIndexOf("&pc=");
		//这步的意思是把如果url中存在pc参数的话，就把pc这个参数去掉，因为上面getPc已经获取pc这个变量值
		if(index!=-1){
			url = url.substring(0, index);
		}
		return url;
	}
	
	
	
	
}
